home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13286 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  56 lines

  1. Path: utpapa.ph.utexas.edu!read
  2. From: read@utpapa.ph.utexas.edu (Dave Read)
  3. Newsgroups: comp.lang.c++
  4. Subject: How does iomanip.h work?
  5. Date: 24 Mar 1996 18:09:29 GMT
  6. Organization: Physics Department, University of Texas at Austin
  7. Message-ID: <4j434p$2nm@geraldo.cc.utexas.edu>
  8. NNTP-Posting-Host: utpapa.ph.utexas.edu
  9.  
  10.  
  11. I want to derive a class from ostream and have it do some special
  12. formatting for me.  I'd like to control the formatting code of
  13. my class the same way you control the formats of other streams with
  14. the code in iomanip.h.  For example, I'd like to do
  15.         mystream << set_my_option(5) << "This is some output." << endl;
  16.  
  17. The problem is, I can't for the life of me figure out how the code in
  18. Borland's iomanip.h does its thing!  Can anyone explain to me how this
  19. stuff works?  For reference, I have included a small segment of the
  20. code from iomanip.h (just enough to see how they do setw() ).
  21.  
  22. I would be extremely grateful for uded a small segment of the
  23. code from iomanip.h (just enough to see how they do setw() ).
  24.  
  25. I would be extremely grateful for any light shed!
  26.  
  27. Thanks,
  28. Dave
  29.  
  30. --
  31.  
  32. template<class typ> class _EXPCLASS smanip {
  33.         ios _FAR & (_RTLENTRY *_fn)(ios _FAR &, typ);
  34.         typ _ag;
  35. public:
  36.         _RTLENTRY smanip(ios _FAR & (_RTLENTRY *_f)(ios _FAR &, typ), typ _a) :
  37.                 _fn(_f), _ag(_a) { }
  38.         friend istream _FAR & _RTLENTRY operator>>(istream _FAR & _s,
  39.         smanip<typ> _FAR & _f) {
  40.                         (*_f._fn)(_s, _f._ag); return _s; }
  41.         friend ostream _FAR & _RTLENTRY operator<<(ostream _FAR & _s,
  42.         smanip<typ> _FAR & _f) {
  43.                         (*_f._fn)(_s, _f._ag); return _s; }
  44.         };
  45.  
  46.  
  47. // set the field width to n
  48.  
  49. smanip<int>     _RTLENTRY _EXPFUNC setw(int _n);
  50.  
  51.  
  52. -- 
  53. Dave Read, Ph.D.  (read@physics.utexas.edu)  "When in doubt, sheet it out."
  54. UT-Austin Heavy Ion Physicist
  55. PDGA #9821                                        G O   B R A V E S ! !
  56.